package com.iflytek.erp.base.controller;

import com.iflytek.erp.base.entity.BaseDataStoreCenter;
import com.iflytek.erp.base.service.BaseDataStoreCenterService;
import com.iflytek.erp.util.ReturnUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
@RestController=@Controller+@ResponseBody
@ResponseBody:表示把返回的值以JSON的格式进行传递给前端
 */

/**
 * 仓库表(BaseDataStoreCenter)表控制层
 */
@RestController
@RequestMapping("base/storeCenter")
public class BaseDataStoreCenterController {

    private final static Logger log = LoggerFactory.getLogger(BaseDataStoreCenterController.class);

    /**
     * 服务对象
     */
    @Resource
    private BaseDataStoreCenterService baseDataStoreCenterService;


    /**
     * 查询仓库列表
     *
     * @param id
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/list")
    public Object list(Integer id, String name, Integer status) {
        //1、调用service进行查询列表
        List<BaseDataStoreCenter> list = baseDataStoreCenterService.list(id, name, status);
        return ReturnUtil.success("查询列表成功", list);
    }


    @GetMapping("/get")
    public Object get(Integer id) {
        if (null == id) {
            return ReturnUtil.fail("id不能为空");
        }
        BaseDataStoreCenter obj = baseDataStoreCenterService.queryById(id);
        return ReturnUtil.success("查询成功", obj);
    }

    @GetMapping("/delete")
    public Object delete(Integer id) {
        if (null == id) {
            return ReturnUtil.fail("id不能为空");
        }
        try {
            boolean b = baseDataStoreCenterService.deleteById(id);
            if (b) {
                return ReturnUtil.success("删除成功");
            } else {
                return ReturnUtil.fail("删除失败");
            }
        } catch (Exception e) {
            log.error("删除仓库异常，{}", e);
            return ReturnUtil.fail("删除失败");
        }
    }

    @GetMapping("/update")
    public Object update(BaseDataStoreCenter obj) {
        if (null == obj) {
            return ReturnUtil.fail("请传输正确的参数");
        }
        BaseDataStoreCenter newObj = baseDataStoreCenterService.update(obj);
        if (newObj == null) {
            return ReturnUtil.success("修改失败，请刷新后重试");
        }

        return ReturnUtil.success("修改成功", newObj);
    }


    @GetMapping("/insert")
    public Object insert(BaseDataStoreCenter obj) {
        if (null == obj||StringUtils.isBlank(obj.getName())||StringUtils.isBlank(obj.getContact())) {
            return ReturnUtil.fail("仓库名称或联系人为空，请填写");
        }
        BaseDataStoreCenter newObj = baseDataStoreCenterService.insert(obj);
        if (newObj == null) {
            return ReturnUtil.success("新增失败，请刷新后重试");
        }

        return ReturnUtil.success("新增成功", newObj);
    }
}
